home *** CD-ROM | disk | FTP | other *** search
/ HPAVC / HPAVC CD-ROM.iso / NRLIBE32.ZIP / EXAMPLE1.C next >
C/C++ Source or Header  |  1994-12-01  |  8KB  |  229 lines

  1. /****************************************************************************/
  2. /*                                                                          */
  3. /*                     Example of NRLIB32 library use                       */
  4. /*                                                                          */
  5. /*  This program make a net population live ; each net has same input ,     */
  6. /*  but it  correct her behaviour by imitating another net random selected. */
  7. /*  Only first <ua> nets don't choose model ; they have correct trainer     */
  8. /*  value from a file .                                                     */
  9. /*                                                                          */
  10. /*  We can see that knoledge diffuse on entire population.                  */
  11. /*                                                                          */
  12. /*  On command line we can choose a different value for any parameeter  or  */
  13. /*  let defaults (see help routine).                                        */
  14. /*                                                                          */
  15. /*  You must compile this program and link it with NRLIB32.OBJ file.        */
  16. /*  Memory model is large.                                                  */
  17. /*                                                                          */
  18. /****************************************************************************/
  19.  
  20. # include "nrlib32.h"       /* Library include                             */
  21.  
  22. # include <stdio.h>
  23. # include <stdlib.h>
  24. # include <math.h>
  25.  
  26. # define NULLL 0L
  27.  
  28. # define TU 500              /* Max number of nets                          */
  29.                  /* Nets definition constants:                  */
  30. # define MLIV   2            /* Last layer                                  */
  31. # define NL0    2            /* Layer 0 nodes                               */
  32. # define NL1    4            /* Layer 1 nodes                               */
  33. # define NL2    1            /* layer 2 nodes                               */
  34.  
  35.  
  36.  
  37. float err[TU+1];             /* Buffers for nets average quadr. errors      */
  38.  
  39. int   teach[TU+1];           /* Flag for net imitator or not                */
  40.  
  41. struct io                    /* Buffers for nets input, output and errors   */
  42. {
  43.   float vinp[NL0+1];         
  44.   float vout[NL2+1];
  45.   float verr[NL2+1];
  46. }         io[TU+1];
  47.  
  48. float vinp[NL0+1];           /* Buffer for input move from trainer file     */
  49. float verr[NL2+1];           /* Buffer for reference output from train. file*/
  50.  
  51.  
  52. int tu=10;                   /* Default nets number (units)                 */
  53. int ua=5;                    /* Default goods nets (have treaning from file)*/
  54. int intvis=50;               /* Print average errors default interval       */
  55. int tlife=5000;              /* Life cycles                                 */
  56.  
  57. char filetrain[]="nettrain.dat";/* Trainer file name                        */
  58.  
  59.  
  60. main(int argc, char *argv[])
  61. {
  62.  int i;
  63.  long sss;
  64.                  /* Modify defaults:                            */
  65.                  /* tu = total nets                             */
  66.                  /* ua = number of goods nets                   */
  67.                  /* tl = life cycles                            */
  68.                  /* iv = print interval                         */
  69.  for (i=1;i<argc;i++)
  70.  {
  71.    if (strncmp(argv[i],"/tu=",4) == 0)  {tu=atoi(argv[i]+4);continue;}
  72.    if (strncmp(argv[i],"/ua=",4) == 0)  {ua=atoi(argv[i]+4);continue;}
  73.    if (strncmp(argv[i],"/tl=",4) == 0)  {tlife=atoi(argv[i]+4);continue;}
  74.    if (strncmp(argv[i],"/iv=",4) == 0)  {intvis=atoi(argv[i]+4);continue;}
  75.    if (strncmp(argv[i],"/h",2) == 0)    {help();exit(0);}
  76.  }
  77.  param("fd","0");            /* No verbose                                  */
  78.  time(&sss);srand((int)sss); /* Init random seed                            */ 
  79.  initialize();               /* Create nets                                 */
  80.  evolution();                /* Nets life                                   */
  81.  end();
  82. }
  83.  
  84.  
  85.  
  86. initialize()
  87. {
  88.  int i,j,nu;
  89.  LONG vn[MLIV+1];
  90.  char vf[MLIV+1][10], va[MLIV+1][10];
  91.                  /* Init array for <createallnets> function     */
  92.    vn[0]=NL0;strcpy(vf[0],"f1");strcpy(va[0],"0");
  93.    vn[1]=NL1;strcpy(vf[1],"f5");strcpy(va[1],"0");
  94.    vn[2]=NL2;strcpy(vf[2],"f5");strcpy(va[2],"0");
  95.                  /* Nets creation                               */
  96.  createallnet(tu,MLIV,vn,vf,va);
  97.  
  98.                  /* Assign flag for model or imitator           */
  99.                  /* and initialize output for first move        */
  100.  for (nu=1;nu<=tu;nu++)
  101.     {
  102.      if (nu <= ua) teach[nu]=1;
  103.      for (j=1;j<=NL0;j++) {io[nu].vinp[j]=rndab(0,1);io[nu].vout[j]=rndab(0,1);};
  104.     }
  105. }
  106.  
  107.  
  108. evolution()
  109. {
  110.  int cl,tvis;
  111.  tvis=0;
  112.    for (cl=1;cl<=tlife;cl++)
  113.    {
  114.      netlife() ;
  115.      tvis++;if (tvis >= intvis){  errshow();tvis=0;  }
  116.    }
  117. }
  118.  
  119.  
  120.  
  121. netlife()
  122. {
  123.  int cv,nu,ret,i,j;
  124.    static FILE *fp;
  125.                  /* Open trainer file for input values and      */
  126.                  /* reference output  values                    */
  127.    if (fp == NULLL)
  128.    {
  129.    fp = fopen(filetrain,"r");
  130.    if (fp == NULLL) {printf("Can't open trainer file \r\n");exit(-1);}
  131.    }
  132.                  /* Input values to <vinp> array                */
  133.      ret=fscanf(fp,"%f",&vinp[1]);
  134.      if  (ret <= 0) {rewind(fp); ret=fscanf(fp,"%f",&vinp[1]);}
  135.      for (i=2;i<=NL0;i++)
  136.      {
  137.        ret=fscanf(fp,"%f",&vinp[i]);
  138.        if  (ret <= 0)
  139.        {printf("Error on trainer file \r\n");exit(-1);}
  140.      }
  141.                  /* Reference output values to <verr> array     */
  142.      for (i=1;i<=NL2;i++)
  143.      {
  144.        ret=fscanf(fp,"%f",&verr[i]);
  145.        if  (ret <= 0)
  146.        {printf("Error on trainer file \r\n");exit(-1);}
  147.      }
  148.  
  149.                  /* Apply input to all nets and compute output */
  150.  for (nu=1;nu<=tu;nu++) { action(nu); }
  151.  
  152.                  /* Apply EBP to all nets                      */
  153.  for (nu=1;nu<=tu;nu++) { ebptrain(nu);  }
  154.  
  155. }
  156.  
  157. action(int nu)
  158. {
  159.    int i;
  160.    float errq;
  161.                  /* Applay input and output reference values    */
  162.    for (i=1;i<=NL0;i++){io[nu].vinp[i]=vinp[i];}
  163.    for (i=1;i<=NL2;i++){io[nu].verr[i]=verr[i];}
  164.                  /* Compute output and evaluate error           */
  165.    errq= compute(nu,io[nu].vinp,io[nu].vout,io[nu].verr);
  166.                  /* averege q. error in return value            */
  167.                  /* and  errors in <verr>                       */
  168.    err[nu]=err[nu]+errq;
  169.    return ;
  170. }
  171.  
  172. ebptrain(int nu)
  173. {
  174.    int um,i,x;
  175.  
  176.                  /* If unit is an imitator , errors are         */
  177.                  /* calculated by output of another unit (model)*/
  178.                  /* randomly choosed                            */
  179.                  /* Else errors remain the same what was        */
  180.                  /* calculated by reference output              */
  181.    if ( teach[nu] != 1 )
  182.     {
  183.      um=rndab(1,tu);
  184.      for (i=1;i<=NL2;i++)
  185.        {io[nu].verr[i]=io[nu].vout[i]-io[um].vout[i];}
  186.     }
  187.                  /* EBP rule                                    */
  188.    learn(nu,io[nu].verr);
  189. }
  190.  
  191.  
  192. errshow()
  193. {
  194.   int i;
  195.                  /* Print <tu> average errors                   */
  196.   for (i=1;i<=tu;i++)
  197.   {
  198.      printf("%7.4f ",err[i]/intvis);err[i]=0;
  199.   }
  200.   printf("\n");fflush(stdout);
  201. }
  202.  
  203.  
  204. end()
  205. {
  206.  cancnets();
  207.  exit(0);
  208. }
  209.  
  210. help() 
  211. {
  212.  printf("\n NETS POPULATION THAT IMIT EACH OTHER \n");
  213.  printf("\n parameters: ");
  214.  printf("\n    /tu=.. : total nets (def = 10)");
  215.  printf("\n    /ua=.. : goods nets (don't imit another net)(def = 5)");
  216.  printf("\n    /tl=.. : life cycles (def = 5000)");
  217.  printf("\n    /iv=.. : printing interval (def = 50)");
  218.  printf("\n ");
  219. }
  220.  
  221.                  /* Integer random value between a and b        */
  222. rndab(int a,int b)
  223. {
  224.   float num;
  225.   num= rand()&0x7FFF; num=num/MAX_RAND;
  226.   return (floor)(num*(b-a)+a+.5);
  227. }
  228.  
  229.